package com.self.campussecondhandmarket.controller.user;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.self.campussecondhandmarket.common.BaseContext;
import com.self.campussecondhandmarket.entity.domain.ForumAnswer;
import com.self.campussecondhandmarket.entity.domain.User;
import com.self.campussecondhandmarket.entity.request.forumAnswer.Answer;
import com.self.campussecondhandmarket.entity.request.forumAnswer.ForumAnswerId;
import com.self.campussecondhandmarket.entity.response.forumAnswer.AnswerAll;
import com.self.campussecondhandmarket.entity.response.forumAnswer.ListWithForumId;
import com.self.campussecondhandmarket.resp.Result;
import com.self.campussecondhandmarket.service.ForumAnswerService;
import com.self.campussecondhandmarket.service.UserService;
import lombok.NonNull;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Author: Vincent
 * <p> DateTime: 2024/4/14 18:35
 * <p> Description: 论坛回答视图
 * <p>
 **/
@RestController
@RequestMapping(("/api/forumAnswer"))
@CrossOrigin(origins = "http://localhost:9999", allowCredentials = "true")
public class ForumAnswerController {
	
	@Autowired
	ForumAnswerService forumAnswerService;
	@Autowired
	UserService userService;
	
	/**
	 * 根据论坛id获取回答列表
	 */
	@PostMapping("/byForumId")
	public Result getListByForumId(@RequestBody @NonNull ForumAnswerId dto) {
		List<ForumAnswer> list = forumAnswerService.list(Wrappers.<ForumAnswer>lambdaQuery()
				.eq(ForumAnswer::getForumId, dto.getForumId())
		);
		List<ListWithForumId> res = list.stream().map(o -> {
			User user = userService.getById(o.getUserId());
			ListWithForumId vo = new ListWithForumId();
			BeanUtils.copyProperties(o, vo);
			vo.setAvatarUrl(user.getAvatarUrl());
			vo.setName(user.getName());
			return vo;
		}).toList();
		return Result.success(res);
	}
	
	/**
	 * 提交回答
	 */
	@PostMapping("/answer")
	public Result answer(@RequestBody @NonNull Answer dto) {
		if (dto.getForumId() == null
				|| dto.getContent() == null
				|| dto.getUserId() == null) {
			return Result.error("参数异常");
		}
		ForumAnswer vo = new ForumAnswer();
		vo.setForumId(dto.getForumId());
		vo.setContent(dto.getContent());
		vo.setUserId(BaseContext.getId());
		forumAnswerService.save(vo);
		return Result.success("评论成功");
	}
	
	// ============ 【管理端】 ================
	
	/**
	 * 获取所有回答 -- 管理端
	 */
	@PostMapping("/admin/all")
	public Result all() {
		List<AnswerAll> list = forumAnswerService.list().stream().map(o -> {
			AnswerAll vo = new AnswerAll();
			User user = userService.getById(o.getUserId());
			vo.setAvatarUrl(user.getAvatarUrl());
			vo.setUserName(user.getName());
			vo.setCreateTime(o.getCreateTime());
			vo.setUpdateTime(o.getUpdateTime());
			vo.setContent(o.getContent());
			vo.setForumId(o.getForumId());
			vo.setId(o.getId());
			return vo;
		}).toList();
		return Result.success(list);
	}
	
	/**
	 * 删除回答
	 */
	@PostMapping("/del/{id}")
	public Result delete(@PathVariable("id") Integer id) {
		forumAnswerService.removeById(id);
		return Result.success("删除成功");
	}
	
	/**
	 * 修改回答
	 */
	@PostMapping("/edit")
	public Result edit(@RequestBody @NonNull AnswerAll dto) {
		ForumAnswer forumAnswer = new ForumAnswer();
		BeanUtils.copyProperties(dto, forumAnswer);
		forumAnswer.setUpdateTime(null);
		forumAnswer.setCreateTime(null);
		forumAnswerService.updateById(forumAnswer);
		return Result.success("修改成功");
	}
	
	/**
	 * 条件查询
	 */
	@PostMapping("/admin/filter")
	public Result filter(@RequestBody @NonNull AnswerAll dto) {
		try {
			List<Integer> users = userService.list(Wrappers.<User>lambdaQuery().eq(User::getName, dto.getUserName())).stream().map(User::getId).toList();
			List<AnswerAll> list = forumAnswerService.list(Wrappers.<ForumAnswer>lambdaQuery()
					.eq(dto.getForumId() != null, ForumAnswer::getForumId, dto.getForumId())
					.in(ForumAnswer::getUserId, users)
			).stream().map(o -> {
				AnswerAll vo = new AnswerAll();
				User user = userService.getById(o.getUserId());
				vo.setAvatarUrl(user.getAvatarUrl());
				vo.setUserName(user.getName());
				vo.setCreateTime(o.getCreateTime());
				vo.setUpdateTime(o.getUpdateTime());
				vo.setContent(o.getContent());
				vo.setForumId(o.getForumId());
				vo.setId(o.getId());
				return vo;
			}).toList();
			return Result.success(list);
			
		} catch (Exception e) {
			return Result.error("无用户");
		}
		
	}
}
